package de.gameduell.framework.facebook.communication {
	import de.gameduell.facebook.service.gen.Balance;
	import de.gameduell.facebook.service.gen.UserProfile;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.locale.LocaleData;
	import de.gameduell.framework.util.FlashVars;

	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class FacebookData extends EventDispatcher {

		public static const FBE_USER_DATA:String = "FBE_USER_DATA";
		public static const FBE_FRIEND_IDS:String = "FBE_FRIEND_IDS";
		public static const FBE_FRIEND_DATA:String = "FBE_FRIEND_DATA";
		public static const FBE_USER_BALANCE:String = "FBE_USER_BALANCE";

		private var _appid:String;
		private var _apikey:String;
		private var _serverPrefix:String;
		private var _sessionId:String;
		private var _userId:String;
		private var _user:UserProfile;
		private var _friendIDs:Array;
		private var _friendProfiles:Array;
		private var localeData:LocaleData;
		private var _userBalance:Balance;

		public function FacebookData(){
			localeData = LocaleData.getInstance();
			_friendIDs = new Array();
			_friendProfiles = null;
			_appid = FlashVars.getKeyValue("appid");
			_apikey = FlashVars.getKeyValue("apikey");
			_serverPrefix = FlashVars.getKeyValue("serverPrefix");
			_sessionId = FlashVars.getKeyValue("sessionId");
			_userId = FlashVars.getKeyValue("userId");
			
			Debug.trace("FacebookData::FacebookData: appid :" + _appid, Debug.FACEBOOK);
			Debug.trace("FacebookData::FacebookData: apikey :" + _apikey, Debug.FACEBOOK);
			Debug.trace("FacebookData::FacebookData: serverPrefix :" + _serverPrefix, Debug.FACEBOOK);
			Debug.trace("FacebookData::FacebookData: sessionId :" + _sessionId, Debug.FACEBOOK);
			Debug.trace("FacebookData::FacebookData: userId :" + _userId, Debug.FACEBOOK);
		}

		public function get userId():String {
			return _userId;
		}
		
		public function get sessionId():String {
			return _sessionId;
		}
		
		public function get serverPrefix():String {
			return _serverPrefix;
		}
		
		public function get apikey():String {
			return _apikey;
		}
		
		public function get appid():String {
			return _appid;
		}
		
		public function get user():UserProfile {
			return _user;
		}

		public function set user(aUser:UserProfile):void {
			_user = aUser;
			localeData.setValue("userName", user.firstName);
			Debug.trace("isFan -> " + user.isFan, Debug.FACEBOOK);
			Debug.trace("bookmarked -> " + user.bookmarked, Debug.FACEBOOK);
			Debug.trace("lastScore -> " + user.lastScore, Debug.FACEBOOK);
			Debug.trace("highscore -> " + user.highscore, Debug.FACEBOOK);
			dispatchEvent(new Event(FBE_USER_DATA));
		}

		public function set friendProfiles(uProfiles:Array):void {
			_friendProfiles = uProfiles;
			_friendProfiles.push(_user);
			dispatchEvent(new Event(FBE_FRIEND_DATA));
		}
		
		public function get friendProfiles():Array {
			return _friendProfiles;
		}
		
		public function getFriendProfileByID(anID:String):UserProfile {
			for each(var up:UserProfile in _friendProfiles){
				if(up.uid == anID){
					return up;
				}
			}
			return null;
		}

		public function get friendIDs():Array {
			return _friendIDs;
		}
		
		public function set friendIDs(aFriendIDs:Array):void {
			_friendIDs = aFriendIDs;
			dispatchEvent(new Event(FBE_FRIEND_IDS));
		}
			
		public function set balance(aBalance:Balance):void {
			Debug.trace("FacebookData::balance -> " + aBalance.balance, Debug.FACEBOOK);
			_userBalance = aBalance;
			dispatchEvent(new Event(FBE_USER_BALANCE));
		}

		public function get balance():Balance {
			return _userBalance;
		}
	}
}
